﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WalkerFire : MonoBehaviour
{
    GameObject arrow;
    float m_FireSpeed;
    float m_FireTimer;
    void Start()
    {
        m_FireSpeed=3;
        m_FireTimer=m_FireSpeed;
    }
    void Update()
    {
        UpdateFireSpeed();
    }
    void OnTriggerStay(Collider target)
    {
        if (m_FireTimer<m_FireSpeed)
        {
            return;
        }       
        if (target.gameObject.tag=="Rider")
        {
             Debug.Log("Near Rider");
            //FireArrow();
            float destinationDistance=Vector3.Distance(this.transform.position, target.transform.position)*Random.Range(1.5f,0.5f);
            //float destinationDistance=Vector3.Distance(target.transform.Position, this.transform.position);
            //Debug.Log("Rider"+ this.transform.position);
            //Debug.Log("Walker" + target.transform.localPosition);
            //Debug.Log(destinationDistance); 
            
            if (PoolManager.Instance.m_ArrowList.Count<1)
            {
                PoolManager.Instance.SpawnArrow(10);
            }
            arrow=PoolManager.Instance.m_ArrowList[0];
            arrow.GetComponent<Arrow>().targetTag=RoleCatagory.None;
            arrow.transform.position=this.transform.position;
            arrow.transform.LookAt(target.transform);
            Vector3 distinationAngle=arrow.transform.eulerAngles;
            distinationAngle.y+=Random.Range(-15,15);
            arrow.transform.eulerAngles=distinationAngle;
            arrow.GetComponent<Arrow>().SetupArrow(destinationDistance, RoleCatagory.Rider);
            PoolManager.Instance.m_ArrowList.Remove(arrow); 
            m_FireTimer=0;  
        }
    }
    void UpdateFireSpeed()
    {
        m_FireTimer+=Time.deltaTime;
    }
}
